package com.action.pc.tag;

import com.bean.Tag;
import com.common.BaseAction;
import com.common.ModelAndJson;
import com.service.tag.TagService;
import com.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by lvjianqing on 2017/9/12.
 */
@Controller
@RequestMapping("tag")
public class TagAction extends BaseAction {
    @Autowired
    private TagService tagService;

    @RequestMapping("province")
    @ResponseBody
    public String getProvinceOptions(String def) {
        List<Tag> list = tagService.findByLv(1, null);
        return getOptins(list, def);
    }

    @RequestMapping("city")
    @ResponseBody
    public String getCityOptions(String name, String def) {
        List<Tag> list = tagService.findByLv(2, name);
        return getOptins(list, def);
    }

    @RequestMapping("region")
    @ResponseBody
    public String getRegionOptions(String name, String def) {
        List<Tag> list = tagService.findByLv(3, name);
        return getOptins(list, def);
    }

    private String getOptins(List<Tag> list, String def) {
        if (list != null && list.size() > 0) {
            StringBuffer sb = new StringBuffer();
            sb.append("<option value=''>请选择</option>");
            for (Tag t : list) {
                String dicName = t.getTname();
                if (StringUtil.isNotEmpty(def) && dicName.equals(def)) {
                    sb.append("<option selected value=\"").append(dicName).append("\">").append(dicName).append("</option>");
                } else {
                    sb.append("<option value=\"").append(dicName).append("\">").append(dicName).append("</option>");
                }
            }
            return sb.toString();
        } else {
            return "<option value=''>暂无数据</option>";
        }
    }


    @ResponseBody
    @RequestMapping("getChildList")
    public String getChildList(ModelAndJson modelMap,Integer pid) {
        List<Tag> list = tagService.findByParentId(pid);
        modelMap.put("tagList",list);
        modelMap.setTrue("");
        return modelMap.toString();
    }


}
